<?php
namespace Framework\Library\Core;

/**
 * 模板类
 * @author kent
 *
 */
class Template{
	
	private $module;
	
	function __construct( $module ){
		$this->module = $module;
	}
	
	public function data( $key = NULL ){
		
		if ($key == NULL) return $this->module ->viewData;
		else {			
			if (empty($this->module ->viewData[$key])) return NULL;
			else return $this->module ->viewData[$key];
		}
		
	}
	
	public function eData( $key = NULL, $f = true ){
		$data = $this->data($key);
		if ($f) $data = htmlspecialchars($data);
		echo $data;
	}
	
	private function assign( $key, $value ){
      
      //检查数组是否已初始化
      if ( !is_array($this->module->viewData) ) $this ->viewData = array();
      
      $this->module->viewData[$key] = $value;
    
    }
	
	public function dispaly( $tpl_name = NULL ){
		
		if ($tpl_name === NULL){
			
			$tpl_name = $this->module ->application ->request ->action;
			
			$tpl_path = $this->module ->application ->path.'/Template/'
					   .$this->module ->application ->request ->module.'/'
					   .$this->module ->application ->request ->action.'.tpl.php';
			
		} else {
			
			$tpl_path = __DIR__.'/../Template/NotFound.tpl.php';
			
		}
		
		//检查视图模板是否存在		
		if ( \file_exists($tpl_path) ) include($tpl_path);
		else{
		
			//输出未渲染结果
			echo '<p>视图「'.$tpl_name.'」不存在，执行结果未经过渲染。</p>'
				.'<p>'.$tpl_path.'</p>';
			\print_r($this->module ->viewData);
		
		}
		
	}
	
}